<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1 class="title">慢慢变富</h1>
        <p class="subtitle">金钱可能流失，但成长的能力、学习的热情是任何人都抢不走的财富</p>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名或邮箱"
            :prefix-icon="User"
            :class="{ 'input-focus': usernameFocus }"
            @focus="usernameFocus = true"
            @blur="usernameFocus = false"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            :class="{ 'input-focus': passwordFocus }"
            @focus="passwordFocus = true"
            @blur="passwordFocus = false"
          />
        </el-form-item>

        <el-form-item>
          <div class="form-actions">
            <el-checkbox v-model="loginForm.remember" class="remember-checkbox">记住我</el-checkbox>
            <el-link type="primary" :underline="false" class="forgot-link">忘记密码？</el-link>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login-btn" :loading="isLoading" @click="handleLogin">
            登录
          </el-button>
        </el-form-item>

        <div class="register-text">
          还没有账号？
          <el-link type="primary" :underline="false" class="register-link">注册</el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

// 定义登录表单类型
interface LoginForm {
  username: string
  password: string
  remember: boolean
}

// 表单实例
const loginFormRef = ref<FormInstance>()

// 登录表单数据
const loginForm = reactive<LoginForm>({
  username: 'super',
  password: '123456',
  remember: true,
})

// 表单验证规则
const loginRules = reactive<FormRules<LoginForm>>({
  username: [{ required: true, message: '请输入用户名或邮箱', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})

// 状态管理
const isLoading = ref<boolean>(false)
const usernameFocus = ref<boolean>(false)
const passwordFocus = ref<boolean>(false)

const router = useRouter()
// 登录处理函数
const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    // 表单验证
    await loginFormRef.value.validate()

    // 模拟登录加载状态
    isLoading.value = true

    // 模拟API请求延迟
    setTimeout(() => {
      isLoading.value = false
      // 实际项目中这里会调用登录API
      console.log('登录成功', loginForm)
      // 登录成功后的逻辑，如路由跳转
      router.push('/overview')
    }, 1500)
  } catch (error) {
    console.error('表单验证失败', error)
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  // background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  background: linear-gradient(180deg, rgba(227, 240, 254, 1) 0%, rgba(246, 249, 255, 1) 100%);
  font-family: 'Noto Sans SC', sans-serif;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 10px 25px -5px rgba(56, 182, 255, 0.2);
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 15px 30px -5px rgba(56, 182, 255, 0.3);
  }
}

.login-header {
  margin-bottom: 30px;
  text-align: center;
  .title {
    font-size: 28px;
    font-weight: bold;
    color: #333;
    margin-bottom: 8px;
  }

  .subtitle {
    color: #666;
    font-size: 14px;
  }
}

.login-form {
  width: 100%;

  .el-form-item {
    margin-bottom: 20px;
  }
}

.input-focus {
  --el-input-border-color: #38b6ff;
  --el-input-focus-border-color: #38b6ff;
  box-shadow: 0 0 0 3px rgba(56, 182, 255, 0.2);
}

.form-actions {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .remember-checkbox {
    font-size: 14px;
    color: #666;
  }

  .forgot-link {
    font-size: 14px;
    color: #38b6ff;

    &:hover {
      color: #1ea7ff;
    }
  }
}

.login-btn {
  width: 100%;
  padding: 10px 0;
  font-size: 16px;
  background-color: #38b6ff;
  border-color: #38b6ff;

  &:hover {
    background-color: #1ea7ff;
    border-color: #1ea7ff;
  }

  &:active {
    background-color: #0d9eff;
    border-color: #0d9eff;
  }
}

.register-text {
  margin-top: 15px;
  font-size: 14px;
  color: #666;
  text-align: center;

  .register-link {
    font-weight: 500;
    color: #38b6ff;
    margin-left: 4px;

    &:hover {
      color: #1ea7ff;
    }
  }
}

// 适配移动端
@media (max-width: 480px) {
  .login-card {
    padding: 20px;
  }

  .login-header .title {
    font-size: 24px;
  }
}
</style>
